package com.dgut.doctorservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dgut.doctorservice.entity.DepartmentInfo;
import com.dgut.doctorservice.entity.department.OneDepartment;
import com.dgut.doctorservice.entity.department.TwoDepartment;
import com.dgut.doctorservice.entity.excel.DepartmentData;
import com.dgut.doctorservice.listener.DepartmentExcelListener;
import com.dgut.doctorservice.mapper.DepartmentInfoMapper;
import com.dgut.doctorservice.service.DepartmentInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dgut
 * @since 2021-09-08
 */
@Service
public class DepartmentInfoServiceImpl extends ServiceImpl<DepartmentInfoMapper, DepartmentInfo> implements DepartmentInfoService {

    @Override
    public void saveDepartments(MultipartFile file,DepartmentInfoService departmentInfoService) {

        try {
            InputStream in = file.getInputStream();
            EasyExcel.read(in, DepartmentData.class,new DepartmentExcelListener(departmentInfoService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public List<OneDepartment> getAllOneAndTwoDepartments() {

        //一级科室
        QueryWrapper<DepartmentInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id","0");
        List<DepartmentInfo> oneList = baseMapper.selectList(wrapper);
        //二级科室
        QueryWrapper<DepartmentInfo> wrapperTwo=new QueryWrapper<>();
        wrapper.ne("parent_id","0");
        List<DepartmentInfo> twoList = this.list(wrapperTwo);

        //进行必要的封装
        List<OneDepartment> finalList = new ArrayList<>();

        for (int i = 0; i < oneList.size(); i++) {
            DepartmentInfo departmentInfo=oneList.get(i);
            OneDepartment oneDepartment=new OneDepartment();
//            oneDepartment.setId(departmentInfo.getId());
//            oneDepartment.setName(departmentInfo.getName());
            BeanUtils.copyProperties(departmentInfo,oneDepartment);//简化，封装好一级科室
            List<TwoDepartment> twoFinalList=new ArrayList<>();
            for (int i1 = 0; i1 < twoList.size(); i1++) {
                DepartmentInfo departmentInfo1 = twoList.get(i1);
                //todo:进行二级科室的添加
                if(departmentInfo1.getParentId().equals(departmentInfo.getId())){
                    TwoDepartment twoDepartment=new TwoDepartment();
                    BeanUtils.copyProperties(departmentInfo1,twoDepartment);
                    twoFinalList.add(twoDepartment);
                }
            }


            oneDepartment.setChildren(twoFinalList);


            finalList.add(oneDepartment);

        }




        return finalList;
    }

    @Override
    public List<DepartmentInfo> secondDepartment() {
        QueryWrapper<DepartmentInfo> finalDepartment=new QueryWrapper<>();
        //二级科室
        QueryWrapper<DepartmentInfo> wrapperTwo=new QueryWrapper<>();
        finalDepartment.ne("parent_id","0");
        List<DepartmentInfo> twoList = this.list(finalDepartment);
        return twoList;
    }
}
